package hirugarrenIterazioa;

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import java.awt.Dimension;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Vector;

public class EI extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private ZerbitzariaInterfazea k;
	private ResultSetTableModel tableModel;
	  public static final String zerbitzuIzena = "sarreraSisteman";
	private EI_IG2 ei2=new EI_IG2(this);

	/**
	 * This is the default constructor
	 */
	public EI() {
		super();
		initialize();
		Box box=Box.createHorizontalBox();
		Box box1=Box.createVerticalBox();
		JLabel jLabel1=new JLabel("Sar ezazu hilabete kopurua:");
		JTable jTable1=new JTable();
		JScrollPane jScrollPane1= new JScrollPane(jTable1,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		final JTextField jTextField1= new JTextField();
		JButton jButton1= new JButton("Gatazkatsuenak erakutsi");
		box1.add(jLabel1);
		box1.add(jTextField1);
		box.add(box1);
		box.add(jButton1);
		try {
			tableModel=new ResultSetTableModel("com.mysql.jdbc.Driver","jdbc:mysql://localhost/eraikikon","ikasle01","ikasle01","SELECT * FROM erabiltzailea");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JTable resultTable= new JTable(tableModel);
		add(box,BorderLayout.NORTH);
		add(new JScrollPane(resultTable),BorderLayout.CENTER);
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				 try{
					 
//				    System.setSecurityManager(new RMISecurityManager());
					  
						//System.setSecurityManager(new RMISecurityManager());
					   if (System.getSecurityManager() ==null){
						   System.setProperty("java.security.policy","client.policy");
							 System.setSecurityManager(new RMISecurityManager());
						}
//				    Zerbitzaria2 k;
				   String zerbIzena = "rmi://localhost/"+zerbitzuIzena;
				   // 	"rmi://IP_Helbidea:PortuZenb/ZerbitzuarenIzena"
				    k = (ZerbitzariaInterfazea)Naming.lookup(zerbIzena);
				    ei2.setZerbitzariInterfazea(k);
				  
				  
				  
				try{

				
					//Kudeatzailea k= new Kudeatzailea();
					Vector<Integer> ik=k.getIrakasleak();
					Vector<IrakasleUkatua> iul= new Vector<IrakasleUkatua>();
					Vector<SarbideEskaera> se = new Vector<SarbideEskaera>();
					IrakasleUkatua iu;
					String s=jTextField1.getText();
					Integer z=(Integer)Integer.parseInt(s);
					for (int i=0;i<ik.size();i++){
					   Integer tx=k.getTxartel2Urte(ik.elementAt(i));
						se= k.getSarbideEskaeraUkatuak(tx,z);
						if(!se.isEmpty()){
						
							iu=new IrakasleUkatua(se.elementAt(0).getErabiltzaileID(),se.size(),se.elementAt(0).getTxartelIrakurgailuId(),tx);
							iul.addElement(iu);
						}
					}

					Collections.sort(iul);
					Collections.reverse(iul);
					ei2.idatziIrakasleak(iul);
					ei2.bidaliTableModel(tableModel);
					ei2.setVisible(true);
					}//try bukaera
				catch (RemoteException ex){
					ex.printStackTrace();
				}
				 }
				 catch (Exception ex) {ex.printStackTrace();}
			}
			
		});
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(531, 247);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}
	public static void main(String [] args){
		  EI proba = new EI();
		  proba.setVisible(true);
		  proba.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  
	  }
}  //  @jve:decl-index=0:visual-constraint="10,10"
